package com.lesserpand.rabbitmq.redis.controller;

import com.lesserpand.rabbitmq.redis.service.DelayedQueueService;
import com.lesserpand.rabbitmq.redis.service.SkillThread;
import com.lesserpand.rabbitmq.redis.utils.MsService;
import com.lesserpand.rabbitmq.redis.service.RedissonSkillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/redis-test")
public class MsTestController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    RedissonSkillService redissonSkillService;

    @Autowired
    DelayedQueueService delayedQueueService;

    @GetMapping("")
    public void startSkill() {
        System.out.println("开始抢购");
        // 模拟放入缓存10件商品
        stringRedisTemplate.opsForValue().set("pronum", "10");

        MsService service = new MsService();

        for (int i = 0; i < 50; i++) {
            Thread thread = new Thread(new SkillThread(redissonSkillService, service, stringRedisTemplate, "MSKEY"));
            thread.start();
        }
    }

    /**
     * 发送延迟队列消息
     */
    @PutMapping("/queue")
    public void queue() {
        delayedQueueService.sendMessageQueue();
    }

    /**
     * 查询延迟队列消息
     *
     * @return
     */
    @GetMapping("/message")
    public String getMessage() {
        return delayedQueueService.getMessageQueue();
    }
}
